package com.itestmini.testplatformbackend.controller;

import com.alibaba.fastjson2.JSONObject;
import com.itestmini.testplatformbackend.common.ServiceException;
import com.itestmini.testplatformbackend.dto.ResultDto;
import com.itestmini.testplatformbackend.dto.UserDto;
import com.itestmini.testplatformbackend.dto.user.AddUserDto;
import com.itestmini.testplatformbackend.dto.user.UpdateUserDto;
import com.itestmini.testplatformbackend.entity.ITestUser;
import com.itestmini.testplatformbackend.service.ITestUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("itest") //get整个类的controller方法前面全部加一个路径前缀
@Tag(name = "ITestUserController接口集", description = "用户相关所有接口")
public class ITestUserController {

    @Autowired
    private ITestUserService iTestUserService;

//    @Value("${iTest.env}") //取自application.properties中的值
//    private String envValue;


    @Operation(summary = "用户注册接口")
    @PostMapping("register")
    public ResultDto<ITestUser> register(@RequestBody AddUserDto addUserDto){
        ITestUser iTestUser = new ITestUser();
        BeanUtils.copyProperties(addUserDto, iTestUser);
        iTestUserService.save(iTestUser);
        log.info(JSONObject.toJSONString(iTestUser));
        return ResultDto.success("注册成功", iTestUser);
    }


    @Operation(summary = "用户登录接口")
    @PostMapping("login") // 简化上面的操作
    public ResultDto<ITestUser> login(@RequestBody UserDto userDto){
        ITestUser iTestUser = new ITestUser();
        BeanUtils.copyProperties(userDto,iTestUser);
        ResultDto<ITestUser> iTestUserResultDto = iTestUserService.login(iTestUser);

        if (userDto.getUsername().contains("error2")){
            throw new NullPointerException();
        }
        if (userDto.getUsername().contains("error")){
            ServiceException.throwEx("用户命中有error");
        }

        return ResultDto.success("success with userDto - " + iTestUserResultDto, iTestUser);
    }


    @Operation(summary = "用户更新接口")
    @PostMapping("update")
    public ResultDto<ITestUser> update(@RequestBody UpdateUserDto updateUserDto){

        if (StringUtils.isEmpty(updateUserDto.getId())){// 这里应该是通过用户获取id，暂时先这样写
            return ResultDto.fail("用户不能为空");
        }
        ITestUser iTestUser = new ITestUser();
        BeanUtils.copyProperties(updateUserDto, iTestUser);
        iTestUserService.update(iTestUser);
        log.info(JSONObject.toJSONString(iTestUser));
        return ResultDto.success("注册成功", iTestUser);
    }

    @Operation(summary = "根据用户名称或者id查询")
    @RequestMapping(value = "getByName", method = RequestMethod.GET)
    public ResultDto<List<ITestUser>> getByName(@RequestParam(value = "userId", required = false) Integer userId, @RequestParam(value = "userName", required = false) String userName){
        ITestUser iTestUser = new ITestUser();
        iTestUser.setUserName(userName);
        iTestUser.setId(userId);
        log.info("request：" + JSONObject.toJSONString(iTestUser));
        return iTestUserService.getByName(iTestUser);
    }

    @Operation(summary = "根据用户id删除")
    @RequestMapping(value = "deleteUser", method = RequestMethod.GET)
    public ResultDto deleteUser(@RequestParam(value = "userId") Integer userId){
        ITestUser iTestUser = new ITestUser();
        iTestUser.setId(userId);
        log.info("request：" + JSONObject.toJSONString(iTestUser));
        return iTestUserService.deleteUser(iTestUser);
    }

    @Operation(summary = "用户查询接口")
    @RequestMapping(value = "getById/{userId}", method = RequestMethod.GET)
    public String getById(@PathVariable("userId") Long userId){
        System.out.println("getUSer:" + userId);
        return "getById:" + userId ;
    }





    /**
     * getById是通过PathVariable路径传参，getByIdV2通过RequestParam注解传参
     * 调用方式：127.0.0.1:8080/itest/getByIdV2?userId=17326&orderId=140044404004
     * 前面的getById调用方式是：127.0.0.1:8080/itest/getById/17326
     * @param userId
     * @return
     */
    @Operation(summary = "根据订单查询接口")
    @GetMapping("/getByIdV2")
    public String getByIdV2(@RequestParam("userId") Long userId, @RequestParam(value = "orderId", required = false) Long orderId){
        System.out.println("getByIdV2:" + userId + ", orderId= " + orderId);
        return "getByIdV2:" + userId + ", orderId= " + orderId;
    }
}
